package com.bytedance.improvingroad.widget

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import com.bytedance.improvingroad.R
import com.bytedance.improvingroad.databinding.FullBottomSheetDialogLayoutBinding
import com.google.android.material.bottomsheet.BottomSheetBehavior
import com.google.android.material.bottomsheet.BottomSheetDialogFragment

/**
 * Created by chenxiong
 * date 12/20/21
 */
class ModalFullBottomSheet: BottomSheetDialogFragment() {
    private var binding: FullBottomSheetDialogLayoutBinding? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = FullBottomSheetDialogLayoutBinding.inflate(layoutInflater)
        initView()
        return binding?.root
    }

    private fun initView() {
        binding?.ivClose?.setOnClickListener{
            dismiss()
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setStyle(STYLE_NORMAL, R.style.BottomSheetDialogBg)
    }

    override fun onStart() {
        super.onStart()
        //拿到系统的Bottom Sheet，固定写法
        val view:FrameLayout = dialog?.findViewById(R.id.design_bottom_sheet)!!
        //拿到与之关联的behavior
        val behavior = BottomSheetBehavior.from(view)
        //设置弹出高度，如果不设置，则停止滑动时会停在默认0的位置
        behavior.peekHeight = 3000
        //设置弹窗大小
        view.layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT
        behavior.state = BottomSheetBehavior.STATE_EXPANDED
        //添加监听
        behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
            override fun onStateChanged(bottomSheet: View, newState: Int) {
                when (newState) {
                    BottomSheetBehavior.STATE_EXPANDED -> {
                    }
                    BottomSheetBehavior.STATE_COLLAPSED -> {
                    }
                    BottomSheetBehavior.STATE_DRAGGING -> {
                    }
                    BottomSheetBehavior.STATE_SETTLING -> {
                    }
                    BottomSheetBehavior.STATE_HIDDEN -> {
                    }
                }
            }

            override fun onSlide(bottomSheet: View, slideOffset: Float) {

            }

        })
    }
    companion object {
        const val TAG = "ModalFullBottomSheet"
    }
}